﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZDSoft.QLMS.Component.SystemSettingComponent;
using NHibernate.Criterion;
using ZDSoft.QLMS.Entity.SystemSetting;
using ZDSoft.QLMS.Web.Helpers;
using ZDSoft.QLMS.Entity;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class RoleController : ControllerBase
    {
        RoleComponent roleComponent = new RoleComponent();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public void Create(string roleName, string isActive, string selectedIDs)
        {
            Role role = new Role();
            role.RoleName = roleName;
            role.IsActive = bool.Parse(isActive);
            role.SystemFunctions = new List<SystemFunction>();
            SystemFunctionComponent sysFunComponent = new SystemFunctionComponent();
            string[] ids = selectedIDs.Split(',');
            foreach (string i in ids)
            {
                if (i != "undefined" && i != "")
                {
                    string tmp = i.Substring(3);
                    role.SystemFunctions.Add(sysFunComponent.FindByID(int.Parse(tmp)));
                }
            }
            role.Create();
        }

        public JsonResult GetAllSysFunctions(int id, int pid, string name, int level)
        {
            IList<SystemFunction> sysFunctions = new SystemFunctionComponent().FindAll();
            IList<ZTreeHelper<Role>.ZTreeNode> sysTree = new List<ZTreeHelper<Role>.ZTreeNode>();
            foreach (SystemFunction sys in sysFunctions)
            {
                int tmpID = int.Parse(id.ToString() + sys.ID.ToString());
                if (sys.Parent == null)
                {
                    if (sys.Childern.Count != 0)
                    {
                        ZTreeHelper<Role>.ZTreeNode parNode = new ZTreeHelper<Role>.ZTreeNode();
                        parNode.id = tmpID;
                        parNode.pid = id;
                        parNode.tld = "sys" + sys.ID;
                        parNode.name = sys.SystemFunctionName;
                        parNode.@checked = false;
                        parNode.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                        parNode.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                        foreach (SystemFunction subSys in sys.Childern)
                        {
                            int subTmpID = int.Parse(tmpID.ToString() + subSys.ID.ToString());
                            ZTreeHelper<Role>.ZTreeNode chiNode = new ZTreeHelper<Role>.ZTreeNode();
                            chiNode.id = subTmpID;
                            chiNode.pid = tmpID;
                            chiNode.tld = "sys" + subSys.ID;
                            chiNode.name = subSys.SystemFunctionName;
                            chiNode.@checked = false;
                            chiNode.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                            chiNode.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                            parNode.children.Add(chiNode);
                        }
                        sysTree.Add(parNode);
                    }
                    else
                    {
                        ZTreeHelper<Role>.ZTreeNode node = new ZTreeHelper<Role>.ZTreeNode();
                        node.id = tmpID;
                        node.pid = id;
                        node.tld = "sys" + sys.ID;
                        node.name = sys.SystemFunctionName;
                        node.@checked = false;
                        node.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                        node.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                        sysTree.Add(node);
                    }
                }
            }
            JsonResult json = this.Json(sysTree, JsonRequestBehavior.AllowGet);
            return json;
        }

        //public string GetAllSysFunctions(int id, int pid, string name, int level)
        //{
        //    string result = "[";
        //    IList<SystemFunction> sysFunctions = new SystemFunctionComponent().FindAll();
        //    foreach (SystemFunction sys in sysFunctions)
        //    {
        //        int tmpID = int.Parse(id.ToString() + sys.ID.ToString());
        //        if (sys.Parent == null)
        //        {
        //            if (sys.Childern.Count != 0)
        //            {
        //                result += "{\"id\":" + tmpID + ",\"pid\":" + id + ",\"tld\":\"sys" + sys.ID + "\",\"name\":\"" + sys.SystemFunctionName
        //                    + "\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/user.png\",\"children\":[";
        //                foreach (SystemFunction subSys in sys.Childern)
        //                {
        //                    int subTmpID = int.Parse(tmpID.ToString() + subSys.ID.ToString());
        //                    result += "{\"id\":" + subTmpID + ",\"pid\":" + tmpID + ",\"tld\":\"sys" + subSys.ID + "\",\"name\":\"" + subSys.SystemFunctionName
        //                    + "\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/course.png\"},";
        //                }
        //                result = result.Substring(0, result.Length - 1);
        //                result += "]},";
        //            }
        //            else
        //            {
        //                result += "{\"id\":" + tmpID + ",\"pid\":" + id + ",\"tld\":\"sys" + sys.ID + "\",\"name\":\"" + sys.SystemFunctionName
        //                    + "\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/user.png\"},";
        //            }
        //        }
        //    }
        //    result = result.Substring(0, result.Length - 1);
        //    result += "]";
        //    return result;
        //}

        public ActionResult Update(int id)
        {
            return View();
        }

        [HttpPost]
        public void Update(int id, string roleName, string isActive, string selectedIDs)
        {
            Role role = roleComponent.FindByID(id);
            role.RoleName = roleName;
            role.IsActive = bool.Parse(isActive);
            string[] ids = selectedIDs.Split(',');
            role.SystemFunctions = null;
            role.SystemFunctions = new List<SystemFunction>();
            SystemFunctionComponent sysComponent = new SystemFunctionComponent();
            foreach (string i in ids)
            {
                if (i != "undefined" && i != "")
                {
                    string tmp = i.Substring(3);
                    role.SystemFunctions.Add(sysComponent.FindByID(int.Parse(tmp)));
                }
            }
            role.Update();
        }

        public JsonResult GetAllSysFunctionsByID(int id, int pid, string name, int level, int roleID)
        {
            IList<SystemFunction> sysFunctions = new SystemFunctionComponent().FindAll();
            IList<SystemFunction> rolesFunctions = roleComponent.FindByID(roleID).SystemFunctions;
            IList<ZTreeHelper<Role>.ZTreeNode> sysTree = new List<ZTreeHelper<Role>.ZTreeNode>();
            foreach (SystemFunction sys in sysFunctions)
            {
                bool sysFlag = false;
                foreach (SystemFunction roleSys in rolesFunctions)
                {
                    if (sys.ID == roleSys.ID)
                    {
                        int tmpID = int.Parse(id.ToString() + sys.ID.ToString());
                        if (sys.Parent == null)
                        {
                            if (sys.Childern.Count != 0)
                            {
                                ZTreeHelper<Role>.ZTreeNode parNode = new ZTreeHelper<Role>.ZTreeNode();
                                parNode.id = tmpID;
                                parNode.pid = id;
                                parNode.tld = "sys" + sys.ID;
                                parNode.name = sys.SystemFunctionName;
                                parNode.@checked = true;
                                parNode.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                                parNode.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                                foreach (SystemFunction nsys in sys.Childern)
                                {
                                    bool subFlag = false;
                                    foreach (SystemFunction subSys in rolesFunctions)
                                    {
                                        if (nsys.ID == subSys.ID)
                                        {
                                            int subTmpID = int.Parse(tmpID.ToString() + subSys.ID.ToString());
                                            ZTreeHelper<Role>.ZTreeNode chiNode = new ZTreeHelper<Role>.ZTreeNode();
                                            chiNode.id = subTmpID;
                                            chiNode.pid = tmpID;
                                            chiNode.tld = "sys" + subSys.ID;
                                            chiNode.name = subSys.SystemFunctionName;
                                            chiNode.@checked = true;
                                            chiNode.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/course.png";
                                            chiNode.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                                            parNode.children.Add(chiNode);
                                            subFlag = true;
                                        }
                                    }
                                    if (subFlag == false)
                                    {
                                        int subTmpID = int.Parse(tmpID.ToString() + nsys.ID.ToString());
                                        ZTreeHelper<Role>.ZTreeNode node = new ZTreeHelper<Role>.ZTreeNode();
                                        node.id = subTmpID;
                                        node.pid = tmpID;
                                        node.tld = "sys" + sys.ID;
                                        node.name = sys.SystemFunctionName;
                                        node.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                                        node.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                                        parNode.children.Add(node);
                                    }
                                }
                                sysTree.Add(parNode);
                            }
                            else
                            {
                                ZTreeHelper<Role>.ZTreeNode node = new ZTreeHelper<Role>.ZTreeNode();
                                node.id = tmpID;
                                node.pid = id;
                                node.tld = "sys" + sys.ID;
                                node.name = sys.SystemFunctionName;
                                node.@checked = true;
                                node.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                                node.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                                sysTree.Add(node);
                            }
                        }
                        sysFlag = true;
                    }
                }
                if (sysFlag == false)
                {
                    int tmpID = int.Parse(id.ToString() + sys.ID.ToString());
                    if (sys.Parent == null)
                    {
                        if (sys.Childern.Count != 0)
                        {
                            ZTreeHelper<Role>.ZTreeNode parNode = new ZTreeHelper<Role>.ZTreeNode();
                            parNode.id = tmpID;
                            parNode.pid = id;
                            parNode.tld = sys.SystemFunctionName;
                            parNode.name = sys.SystemFunctionName;
                            parNode.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                            parNode.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                            foreach (SystemFunction subSys in sys.Childern)
                            {
                                int subTmpID = int.Parse(tmpID.ToString() + subSys.ID.ToString());
                                ZTreeHelper<Role>.ZTreeNode chiNode = new ZTreeHelper<Role>.ZTreeNode();
                                chiNode.id = subTmpID;
                                chiNode.pid = tmpID;
                                chiNode.tld = "sys" + subSys.ID;
                                chiNode.name = subSys.SystemFunctionName;
                                chiNode.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/course.png";
                                chiNode.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                                parNode.children.Add(chiNode);
                            }
                            sysTree.Add(parNode);
                        }
                        else
                        {
                            ZTreeHelper<Role>.ZTreeNode node = new ZTreeHelper<Role>.ZTreeNode();
                            node.id = tmpID;
                            node.pid = id;
                            node.tld = "sys" + sys.ID;
                            node.name = sys.SystemFunctionName;
                            node.icon = "../../Scripts/zTree/css/zTreeStyle/img/diy/user.png";
                            node.children = new List<ZTreeHelper<Role>.ZTreeNode>();
                            sysTree.Add(node);
                        }
                    }
                }
            }
            JsonResult json = this.Json(sysTree, JsonRequestBehavior.AllowGet);
            return json;
        }

        //public string GetAllSysFunctionsByID(int id, int pid, string name, int level, int roleID)
        //{
        //    string result = "[";
        //    IList<SystemFunction> sysFunctions = new SystemFunctionComponent().FindAll();
        //    IList<SystemFunction> rolesFunctions = roleComponent.FindByID(roleID).SystemFunctions;
        //    foreach (SystemFunction sys in sysFunctions)
        //    {
        //        bool sysFlag = false;
        //        foreach (SystemFunction roleSys in rolesFunctions)
        //        {
        //            if (sys.ID == roleSys.ID)
        //            {
        //                int tmpID = int.Parse(id.ToString() + sys.ID.ToString());
        //                if (sys.Parent == null)
        //                {
        //                    if (sys.Childern.Count != 0)
        //                    {
        //                        result += "{\"id\":" + tmpID + ",\"pid\":" + id + ",\"tld\":\"sys" + sys.ID + "\",\"name\":\"" + sys.SystemFunctionName
        //                            + "\",\"checked\":\"true\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/user.png\",\"children\":[";
        //                        foreach (SystemFunction nsys in sys.Childern)
        //                        {
        //                            bool subFlag = false;
        //                            foreach (SystemFunction subSys in rolesFunctions)
        //                            {
        //                                if (nsys.ID == subSys.ID)
        //                                {
        //                                    int subTmpID = int.Parse(tmpID.ToString() + subSys.ID.ToString());
        //                                    result += "{\"id\":" + subTmpID + ",\"pid\":" + tmpID + ",\"tld\":\"sys" + subSys.ID + "\",\"name\":\"" + subSys.SystemFunctionName
        //                                    + "\",\"checked\":\"true\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/course.png\"},";
        //                                    subFlag = true;
        //                                }
        //                            }
        //                            if (subFlag == false)
        //                            {
        //                                int subTmpID = int.Parse(tmpID.ToString() + nsys.ID.ToString());
        //                                result += "{\"id\":" + subTmpID + ",\"pid\":" + tmpID + ",\"tld\":\"sys" + nsys.ID + "\",\"name\":\"" + nsys.SystemFunctionName
        //                                    + "\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/course.png\"},";
        //                            }
        //                        }
        //                        result = result.Substring(0, result.Length - 1);
        //                        result += "]},";
        //                    }
        //                    else
        //                    {
        //                        result += "{\"id\":" + tmpID + ",\"pid\":" + id + ",\"tld\":\"sys" + sys.ID + "\",\"name\":\"" + sys.SystemFunctionName
        //                            + "\",\"checked\":\"true\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/user.png\"},";
        //                    }
        //                }
        //                sysFlag = true;
        //            }
        //        }
        //        if (sysFlag == false)
        //        {
        //            int tmpID = int.Parse(id.ToString() + sys.ID.ToString());
        //            if (sys.Parent == null)
        //            {
        //                if (sys.Childern.Count != 0)
        //                {
        //                    result += "{\"id\":" + tmpID + ",\"pid\":" + id + ",\"tld\":\"sys" + sys.ID + "\",\"name\":\"" + sys.SystemFunctionName
        //                        + "\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/user.png\",\"children\":[";
        //                    foreach (SystemFunction subSys in sys.Childern)
        //                    {
        //                        int subTmpID = int.Parse(tmpID.ToString() + subSys.ID.ToString());
        //                        result += "{\"id\":" + subTmpID + ",\"pid\":" + tmpID + ",\"tld\":\"sys" + subSys.ID + "\",\"name\":\"" + subSys.SystemFunctionName
        //                        + "\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/course.png\"},";
        //                    }
        //                    result = result.Substring(0, result.Length - 1);
        //                    result += "]},";
        //                }
        //                else
        //                {
        //                    result += "{\"id\":" + tmpID + ",\"pid\":" + id + ",\"tld\":\"sys" + sys.ID + "\",\"name\":\"" + sys.SystemFunctionName
        //                        + "\",\"icon\":\"../../Scripts/zTree/css/zTreeStyle/img/diy/user.png\"},";
        //                }
        //            }
        //        }
        //    }
        //    result = result.Substring(0, result.Length - 1);
        //    result += "]";
        //    return result;
        //}

        [HttpPost]
        public void Withdraw(int id)
        {
            Role role = roleComponent.FindByID(id);
            if (role.IsActive == true)
            {
                role.IsActive = false;
            }
            else
            {
                role.IsActive = true;
            }
            role.Update();
        }

        Role role = null;

        public string GetRoleByID(int id)
        {
            role = roleComponent.FindByID(id);
            IList<KeyValuePair<string, InvokeMethod>> list = new List<KeyValuePair<string, InvokeMethod>>();
            //list.Add(new KeyValuePair<string, InvokeMethod>("xx", GetUsers));
            string str = role.ToJsonString<Role>(list);
            return str;
        }

        //public string GetUsers()
        //{
        //    return role.Users[0].Account;
        //}

        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
            string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Role> list = new List<Role>();
            IList<Role> roles = roleComponent.GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Role role in roles)
            {
                role.ToJaonResult<Role>();
                list.Add(role);
            }
            list.Add(new Role() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }

    }
}